package org.yf.samples.dao;

import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.yf.samples.model.ExamUserRole;
import org.yf.samples.model.User;

@Repository
@Transactional
public class ExamUserRoleDAOImpl extends BaseHibernateDAO<ExamUserRole> implements IExamUserRoleDAO {

	@Override
	public List<ExamUserRole> findAllExamUserRoles() {
		return this.find("FROM ExamUserRole");
	}

	@Override
	public ExamUserRole getExamUserRoleByUserId(String id) {
		return this.queryForObject("FROM ExamUserRole AS r WHERE r.user.userId=? ", new Object[]{id});
	}

	@Override
	public void saveExamUserRoles(List<ExamUserRole> examUserRoleList) {
		this.save(examUserRoleList);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> findAllStudents() {
		String hql = " SELECT r.user AS user FROM ExamUserRole AS r WHERE r.examRole.name='学生' ";
		return this.createQuery(hql).list();
	}

	@Override
	public void createUserRole(ExamUserRole userRole) {
		this.save(userRole);
	}

	@Override
	public void deleteUserRole(String userId) {
		String hql = " DELETE FROM ExamUserRole AS eur WHERE eur.user.userId= :userId ";
		this.createQuery(hql).setString("userId", userId).executeUpdate();
	}
}
